/*
 * Filename:    AuditDownFileController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-12-10
 */
package com.paic.mhis.hcpms.audit.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.hcpms.audit.biz.service.ICaseCheckService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dto.AuditCaseReportDTO;
import com.paic.mhis.hcpms.audit.dto.AuditNotificationDTO;
import com.paic.mhis.hcpms.audit.util.GetFormDateUtil;
import com.paic.mhis.hcpms.itextpdf.web.contoller.HcpmsOperateFile;
import com.paic.mock.util.StringUtils;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

@Controller
@RequestMapping("/auditDownFileController/")
public class AuditDownFileController {
	@BizServicesProxy(beanName = "caseCheckServcieImpl")
	private ICaseCheckService caseCheckService;

	@Value("${fileDownloadPath}")
	private String filePath;

	/**
	 * 下载稽核报告的pdf
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("downReportNoticPdfFile.do")
	@ResponseBody
	public void downReportPdfFile(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String caseCode = request.getParameter("caseCode");
		AuditCaseReportDTO returnBean = this.caseCheckService
				.getAuditCaseReportDTOByCaseCode(caseCode);
		AuditNotificationDTO auditNotification = this.caseCheckService
				.getAuditNotificationDTO(caseCode);
		Map<String, Object> map = this.convertAuditReportInfo(returnBean);
		map.put("title", returnBean.getRegionName() + "医保定点医疗机构、定点零售药店稽核报告");
		HcpmsOperateFile notice = new HcpmsOperateFile(filePath, caseCode
				+ "稽核报告.pdf");

		map.put(AuditConstants.sealDate, auditNotification.getSealDate());
		notice.careatePdfFile(new AuditReportDocument(), map);
		notice.openPdfFile(response);
	}

	private Map<String, Object> convertAuditReportInfo(
			AuditCaseReportDTO returnBean) {
		Date processDate = new Date();
		String processDateStr = GetFormDateUtil.getStrFromDate(processDate);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(AuditConstants.auditDate, returnBean.getAuditDate());
		map.put(AuditConstants.auditRangDate, returnBean.getAuditRang());
		map.put(AuditConstants.auditSummary, returnBean.getAuditSummary());
		map.put(AuditConstants.opinionDesc, returnBean.getProcessOpinion());
		map.put(AuditConstants.processDate, processDateStr);

		return map;

	}

	/**
	 * 下载稽核告知函的pdf
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("downPdfFile.do")
	@ResponseBody
	public void downTellReportPdfFile(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String caseCode = request.getParameter("caseCode");
		AuditNotificationDTO returnBean = this.caseCheckService
				.getAuditNotificationDTO(caseCode);
		returnBean.setCaseCode(caseCode);
		Map<String, Object> map = this.getInfoMap(returnBean);
		map.put("title", returnBean.getRegionName() + "医保定点医疗机构、定点零售药店稽核告知函");
		HcpmsOperateFile notice = new HcpmsOperateFile(filePath, caseCode
				+ "稽核告知函.pdf");
		notice.careatePdfFile(new AuditDocument(), map);
		notice.openPdfFile(response);
		// notice.DownPdfFile(new AuditDocument(), map, response);

	}

	private Map<String, Object> getInfoMap(AuditNotificationDTO bean) {

		Date processDate = new Date();

		String processDateStr = GetFormDateUtil.getStrFromDate(processDate);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put(AuditConstants.processDate, processDateStr);

		String caseCode = bean.getCaseCode();
		String currentCaseCode = caseCode.replace("JH", "J H");

		map.put(AuditConstants.CASE_CODE, currentCaseCode);

		map.put(AuditConstants.healthcareProviderName,
				bean.getHealthcareProviderName());
		map.put(AuditConstants.lealPerson, bean.getLealPerson());
		map.put(AuditConstants.fullAddress, bean.getFullAddress());
		map.put(AuditConstants.pointStartDate, bean.getPointStartDate());
		map.put(AuditConstants.auditDate, bean.getAuditDate());

		String start = bean.getPeriodBeginDate();
		String end = bean.getPeriodEndDate();
		if (StringUtils.hasText(start) && StringUtils.hasText(end)) {
			map.put(AuditConstants.auditRangDate, start + "-" + end);
		} else {
			map.put(AuditConstants.auditRangDate, "");
		}

		map.put(AuditConstants.auditSummary, bean.getAuditSummary());
		map.put(AuditConstants.opinionDesc, bean.getOpinionDesc());
		map.put(AuditConstants.sealDate, bean.getSealDate());
		return map;
	}
}
